package com.qianniu.napi.admin.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.qianniu.napi.admin.entity.GoodsSku;
import com.qianniu.napi.admin.entity.User;
import com.qianniu.napi.admin.service.IGoodsSkuService;
import com.qianniu.napi.common.util.ObjectUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.Date;

/**
 * <p>
 * 商品信息
 * </p>
 *
 * @author chenxuebin
 * @since 2018-09-08
 */
@Controller
public class GoodsSkuController {

    private static final Logger logger = LoggerFactory.getLogger(GoodsSkuController.class);

    @Autowired
    private IGoodsSkuService goodsskuService;

    @RequestMapping("admin/goodssku/index")
    public String index() {
        return "admin/goodssku";
    }

    @RequestMapping("/admin/goodssku/list")
    @ResponseBody
    public Page<GoodsSku> list(@RequestParam(value = "limit", required = false) int limit,
                               @RequestParam(value = "offset", required = false) int offset) {
        User user = ObjectUtil.getSessionUser();
        Wrapper<GoodsSku> wrapper = new EntityWrapper<GoodsSku>().eq("mid", user.getMid());
//        if(user.getStoreId()!=null){
//            wrapper.eq("mpid",user.getStoreId());
//        }
        wrapper.orderBy("id", false);
        Page<GoodsSku> page = goodsskuService.selectPage(new Page<GoodsSku>(offset / limit + 1, limit), wrapper);
        return page;
    }

    @RequestMapping("/admin/goodssku/add")
    public String add(@Valid GoodsSku bean, BindingResult bindingResult) {
        // 字段校验
        if (bindingResult.hasErrors()) {
            logger.debug("There are errors! {}", bindingResult);
            return null;
        }
        bean.setCreatetime(new Date());
        bean.setMid(ObjectUtil.getMid());
        bean.setMpid(ObjectUtil.getMpid());
        bean.setSid(ObjectUtil.getSid());
        bean.setUid(ObjectUtil.getUid());
        bean.insert();
        return "admin/goodssku";
    }

    @RequestMapping("/admin/goodssku/getGoodsSkuById")
    @ResponseBody
    public GoodsSku getGoodsSkuById(long id) {
        GoodsSku goodssku = goodsskuService.selectById(id);
        return goodssku;
    }

    @RequestMapping("/admin/goodssku/deleteOne")
    @ResponseBody
    public int deleteOne(long id) {
        boolean isSucc = goodsskuService.deleteById(id);
        return isSucc ? 1 : 0;
    }

    @RequestMapping("/admin/goodssku/changeState")
    @ResponseBody
    public String changeState(long id, int state) {
        GoodsSku bean = new GoodsSku();
        bean.setId(id);
        bean.setState(state);
        bean.setModifytime(new Date());
        bean.setModifier(ObjectUtil.getUid());
        goodsskuService.updateById(bean);
        return "success";
    }

    @RequestMapping("/admin/goodssku/modify")
    public String update(GoodsSku bean) {
        bean.setModifytime(new Date());
        bean.setModifier(ObjectUtil.getUid());
        boolean isSuccc = goodsskuService.updateById(bean);
        if (isSuccc) {
            return "redirect:index";
        }
        return "error";
    }

}
